package cn.com.bsoft.cloud.utils.test;

import java.text.SimpleDateFormat;
import java.util.*;

public class DateUtil {

    private static final long TICKS_AT_EPOCH = 621355968000000000L;
    private static final long TICKS_PER_MILLISECOND = 10000;
    private static TimeZone timeZone = TimeZone.getDefault();

    /**
     * 时间戳转换成日期格式字符串
     * @param seconds 精确到秒的字符串
     * @param formatStr
     * @return
     */
    public static String timeStamp2Date(String seconds,String format) {
        if(seconds == null || seconds.isEmpty() || seconds.equals("null")){
            return "";
        }
        if(format == null || format.isEmpty()){
            format = "yyyy-MM-dd HH:mm:ss";
        }
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        return sdf.format(new Date(Long.valueOf(seconds+"000")));
    }
    /**
     * 日期格式字符串转换成时间戳
     * @param date 字符串日期
     * @param format 如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    public static String date2TimeStamp(String date_str,String format){
        try {
            SimpleDateFormat sdf = new SimpleDateFormat(format);
            return String.valueOf(sdf.parse(date_str).getTime()/1000);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }

    /**
     * 取得当前时间戳（精确到秒）
     * @return
     */
    public static String timeStamp(){
        long time = System.currentTimeMillis();
        String t = String.valueOf(time/1000);
        return t;
    }


    public static String fromDnetToJdate(long longTime) {
        long test = (longTime - TICKS_AT_EPOCH);
        System.out.println(test);
        Calendar calendar = Calendar.getInstance(timeZone);
        calendar.setTimeInMillis((longTime - TICKS_AT_EPOCH) / TICKS_PER_MILLISECOND);
        calendar.setTimeInMillis(calendar.getTimeInMillis() - calendar.getTimeZone().getRawOffset());
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String str= simpleDateFormat.format(calendar.getTime());
        return str;
    }


    public static String timeStampToDotNet(String timeStamp){
        long longTimeStamp= Long.valueOf(timeStamp).longValue() *10;
        long longTime = longTimeStamp + TICKS_AT_EPOCH;
        String longTimeString = String.valueOf(longTime);

        return longTimeString;

    }



    public static void main(String[] args) {
        long time = System.currentTimeMillis();
        String timeStamp = timeStamp();
        System.out.println("timeStamp="+timeStamp); //运行输出:timeStamp=1470278082
        System.out.println(System.currentTimeMillis());//运行输出:1470278082980
        //该方法的作用是返回当前的计算机时间，时间的表达格式为当前计算机时间和GMT时间(格林威治时间)1970年1月1号0时0分0秒所差的毫秒数

        String date = timeStamp2Date(timeStamp, "yyyy-MM-dd HH:mm:ss");
        System.out.println("date="+date);//运行输出:date=2016-08-04 10:34:42

        String timeStamp2 = date2TimeStamp(date, "yyyy-MM-dd HH:mm:ss");
        System.out.println(timeStamp2);  //运行输出:1470278082

        String timeStamp3 = timeStampToDotNet(timeStamp);
        System.out.println(time);
    }

}